package org.demo.five;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;

/**
 * 
 * @author  
 * @date    2010-10-24
 * @file    org.demo.five.FileUtils.java
 */
public class FileUtils {
	public static Iterable<String> readLines(String filename) throws FileNotFoundException{
		final FileReader fr = new FileReader(filename);
		final BufferedReader br = new BufferedReader(fr);
		return new Iterable<String>(){
			public Iterator<String> iterator() {
				return new Iterator<String>() {
					String line = getLine();
					
					public boolean hasNext() {
						return line != null;
					}
					public String next() {
						String retval = line;
						line = getLine();
						return retval;
					}
					public void remove() {
						throw new UnsupportedOperationException();
					}
					String getLine(){
						String line = null;
						try {
							line = br.readLine();
						} catch (Exception e) {
							line = null;
						}
						if (line == null){
							try { br.close(); } catch (Exception e){}
						}
						return line;
					}
				};
			}
		};
	}
}
